package monitora.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import monitora.dao.Formulario1DAO;
import monitora.dao.Formulario2DAO;


/**
 * Servlet implementation class AdministradorServlet
 */
@WebServlet(name = "administrador", urlPatterns = { "/sisagre/administrador" })
public class AdministradorServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if (!this.logado(request, response)) {
			return;
		}
		
		request.setCharacterEncoding("UTF-8");
		
		int acao = Integer.valueOf(request.getParameter("acao"));
		
		switch (acao) {
			case 1:
				this.exibirItems(request, response);
				break;
			case 2:
				this.exibirFiltro(request, response);
				break;
		}
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.service(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.service(request, response);
	}
	
	private boolean logado(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if (! new LoginPrefeiruaServlet().logado(request, response)) {
			request.getRequestDispatcher("loginprefeitura?acao=2").forward(request, response);
			return false;
		}
		return true;
	}
	
	private void exibirItems(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int formulario = Integer.valueOf(request.getParameter("formulario"));
		String[] items = null;
		
		/*switch (formulario) {
			case 1:
				items = Formulario1DAO.ITEMS;
				break;
			case 2:
				items = Formulario2DAO.ITEMS;
				break;
		}*/
		
		request.setAttribute("formulario", formulario);
		request.setAttribute("items", items);
		
		request.getRequestDispatcher("administrador.jsp").forward(request, response);
	}
	
	private void exibirFiltro(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int formulario = Integer.valueOf(request.getParameter("formulario"));
		int item = Integer.valueOf(request.getParameter("item"));
		String[] items = null;
		
		List<String> lista = null;
		String caminho = "";
		
		/*switch (formulario) {
			case 1:
				lista = new Formulario1DAO().filtro(item);
				caminho = "formulario1";
				items = Formulario1DAO.ITEMS;
				break;
			case 2:
				lista = new Formulario2DAO().filtro(item);
				caminho = "formulario2";
				items = Formulario2DAO.ITEMS;
				break;
		}*/
		
		request.setAttribute("formulario", formulario);
		request.setAttribute("item", item);
		request.setAttribute("items", items);
		
		request.setAttribute("lista", lista);
		request.setAttribute("quantidade", lista.size());
		request.setAttribute("caminho", caminho);
		
		request.setAttribute("usuario", request.getSession().getAttribute("usuario"));
		
		request.getRequestDispatcher("administrador.jsp").forward(request, response);
	}
}
